package com.ruoyi.app.controller.message.service.Impl;

import com.ruoyi.app.controller.member.mapper.IMemberCreditRecordMapper;
import com.ruoyi.app.controller.message.mapper.IAppletTemplateMapper;
import com.ruoyi.app.controller.message.service.IAppletTemplateService;
import com.ruoyi.app.controller.order.mapper.IMemberRechargeOrderMapper;
import com.ruoyi.common.constant.OrderConstants;
import com.ruoyi.common.constant.SysConfigConstants;
import com.ruoyi.common.entity.DO.AppletTemplateDO;
import com.ruoyi.common.entity.DO.member.MemberCreditRecordDO;
import com.ruoyi.common.entity.DO.order.MemberRechargeOrderDO;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;

@Service
public class AppletTemplateServiceImpl implements IAppletTemplateService {

    @Resource
    private IAppletTemplateMapper appletTemplateMapper;

    @Resource
    private IMemberRechargeOrderMapper memberRechargeOrderMapper;

    @Resource
    private IMemberCreditRecordMapper creditRecordMapper;

    @Override
    public String selectTemplateIdByKey(String templateKey) {
        Example example = new Example(AppletTemplateDO.class);
        example.createCriteria().andEqualTo("templateKey", templateKey);
        AppletTemplateDO appletTemplate = appletTemplateMapper.selectOneByExample(example);
        return StringUtils.isNotNull(appletTemplate) ? appletTemplate.getTemplateId() : "";
    }

    public JSONObject getRechargeSuccessNotifyTemplateData(HashMap<String, String> params) {
        JSONObject jsonObject = new JSONObject();
        String thing1;
        String amount3;
        String time2;
        String character_string7;
        String thing13 = "如对订单有疑问，请联系商家确认";
        if ("Y".equals(params.get("isInfo"))) {
            thing1 = params.get("orderStatusDesc");
            amount3 = params.get("amount");
            time2 = params.get("payTime");
            character_string7 = params.get("orderSn");
            if (params.containsKey("creditRemark")) {
                thing13 = params.get("creditRemark");
            }
        } else {
            MemberRechargeOrderDO rechargeOrderInfo = memberRechargeOrderMapper.selectByPrimaryKey(Long.valueOf(params.get("orderId")));
            if (rechargeOrderInfo == null) {
                throw new BusinessException("没有找到此充值订单");
            }
            thing1 = OrderConstants.CreditOrderStatusEnum.match(rechargeOrderInfo.getOrderStatus()).getValueDesc();
            amount3 = rechargeOrderInfo.getAmount().toString();
            time2 = DateFormatUtils.format(rechargeOrderInfo.getPayTime(), "yyyy/MM/dd");
            character_string7 = rechargeOrderInfo.getOrderSn();
            if (rechargeOrderInfo.getGiftCredit() > 0) {
                MemberCreditRecordDO creditRecordDO = creditRecordMapper.getRecordByLinkSn(SysConfigConstants.SystemModuleEnum.RECHARGE.getValue(), rechargeOrderInfo.getOrderSn());
                if (null != creditRecordDO) {
                    thing13 = creditRecordDO.getRemark();
                }
            }
        }
        JSONObject jsonObjectTmp = new JSONObject();
        jsonObjectTmp.put("value", thing1);
        jsonObject.put("thing1", jsonObjectTmp); //充值状态

        jsonObjectTmp = new JSONObject();
        jsonObjectTmp.put("value", amount3);
        jsonObject.put("amount3", jsonObjectTmp); //充值金额

        jsonObjectTmp = new JSONObject();
        jsonObjectTmp.put("value", time2);
        jsonObject.put("time2", jsonObjectTmp); //充值时间

        jsonObjectTmp = new JSONObject();
        jsonObjectTmp.put("value", character_string7);
        jsonObject.put("character_string7", jsonObjectTmp); //订单编号

        jsonObjectTmp = new JSONObject();
        jsonObjectTmp.put("value", thing13);
        jsonObject.put("thing13", jsonObjectTmp);//备注（备注用来显示赠送多少积分，总积分多少）

        return jsonObject;
    }

    @Override
    public JSONObject getPetrolSuccessNotifyTemplateData(HashMap<String, String> params) {
        JSONObject jsonObject = new JSONObject();
        String thing1; //站点
        String amount3;//金额
        String time4; //加油时间
        String thing5="如对订单有疑问，请联系商家确认";//备注
//        if("Y".equals(params.get("isInfo"))){
        thing1 = params.get("shopName");
        amount3 = params.get("payCost");
        time4 = params.get("payTime");
        if (params.containsKey("creditRemark")) {
            thing5 = params.get("creditRemark");
        }
//        }
        JSONObject jsonObjectTmp = new JSONObject();
        jsonObjectTmp.put("value", thing1);
        jsonObject.put("thing1", jsonObjectTmp);  //站点
        jsonObjectTmp = new JSONObject();
        jsonObjectTmp.put("value", amount3);
        jsonObject.put("amount3", jsonObjectTmp); //金额
        jsonObjectTmp = new JSONObject();
        jsonObjectTmp.put("value", time4);
        jsonObject.put("time4", jsonObjectTmp); //加油时间
        jsonObjectTmp = new JSONObject();
        jsonObjectTmp.put("value", thing5);
        jsonObject.put("thing5", jsonObjectTmp); //备注
        return jsonObject;
    }

    @Override
    public JSONObject getBalanceChangeNotifyTemplateData(HashMap<String, String> params) {
        JSONObject jsonObject = new JSONObject();
        String thing5; //变动类型
        String character_string2;//变动金额
        String time1; //变动时间
        String amount3; //当前余额
        String thing4="如对余额变动有疑问，请联系商家确认";//备注
//        if("Y".equals(params.get("isInfo"))){
        thing5 = params.get("linkModelDesc");
        character_string2 = params.get("changeBalance");
        time1 = params.get("createTime");
        amount3 = params.get("newBalance");
//        if (params.containsKey("balanceChangeRemark")) {
//            thing4 = params.get("balanceChangeRemark");
//        }
//        }
        JSONObject jsonObjectTmp = new JSONObject();
        jsonObjectTmp.put("value", thing5);
        jsonObject.put("thing5", jsonObjectTmp);  //变动类型

        jsonObjectTmp = new JSONObject();
        jsonObjectTmp.put("value", character_string2);
        jsonObject.put("character_string2", jsonObjectTmp); //变动金额

        jsonObjectTmp = new JSONObject();
        jsonObjectTmp.put("value", amount3);
        jsonObject.put("amount3", jsonObjectTmp); //当前余额

        jsonObjectTmp = new JSONObject();
        jsonObjectTmp.put("value", time1);
        jsonObject.put("time1", jsonObjectTmp); //充值时间

        jsonObjectTmp = new JSONObject();
        jsonObjectTmp.put("value", thing4);
        jsonObject.put("thing4", jsonObjectTmp); //备注
        return jsonObject;
    }

}
